//
//  EmoticonKeyBoardView.swift
//  EmoticonKeyBoard
//
//  Created by yang on 15/11/21.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit
private let EmoticonCellId = "EmoticonCellId"
private let toolBarHeight : CGFloat = 36
class EmoticonKeyBoardView: UIView {
    //闭包属性
    var selectEmoticon :((emoticon:Emoticon) -> ())?
    //tooBal 的点击方法
    @objc private func itemClick(item:UIBarButtonItem){
        let indexPath = NSIndexPath(forItem: 0, inSection: item.tag)
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: true)    
    }
    //重写构造方法
    init(selectEmoticon :((emoticon:Emoticon) -> ())){

        self.selectEmoticon = selectEmoticon
        let rect = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 220);
        super.init(frame:rect)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //懒加载
    private lazy var packages = EmoticonManager().packages
    private lazy var toolBar = UIToolbar()
    private lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        let w = UIScreen.mainScreen().bounds.width / 7
        layout.itemSize = CGSize(width: w, height: w)
        let margin = (self.bounds.height - 3 * w - toolBarHeight) / 4
        layout.sectionInset = UIEdgeInsets(top: margin, left: 0, bottom: margin, right: 0)
        let v = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        v.backgroundColor = UIColor.whiteColor()
        v.registerClass(EmoticonCell.self, forCellWithReuseIdentifier: EmoticonCellId)
        v.pagingEnabled = true
        v.dataSource = self
        v.delegate = self
        return v
    }()
}
//UICollectionView 的数据源方法和代理方法
extension EmoticonKeyBoardView :UICollectionViewDataSource,UICollectionViewDelegate{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int{
        return packages.count
    }
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return packages[section].emoticonList.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCellId, forIndexPath: indexPath) as! EmoticonCell
        cell.emoticon = packages[indexPath.section].emoticonList[indexPath.item]
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let emoticon = packages[indexPath.section].emoticonList[indexPath.item]
        selectEmoticon?(emoticon:emoticon)
    }
}
//设置 UI
extension EmoticonKeyBoardView{
  private  func setupUI(){
        addSubview(toolBar)
        addSubview(collectionView)
    
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.right.left.equalTo(self)
            make.height.equalTo(toolBarHeight)
        }
    
        collectionView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        setToolBar()
    }
   private func setToolBar(){
        toolBar.tintColor = UIColor.lightGrayColor()
        var items = [UIBarButtonItem]()
        var index = 0
        for package in packages {
            
            let item = UIBarButtonItem(title:package.group_name_cn, style: .Plain, target: self, action: "itemClick:")
            items.append(item)
            items.last?.tag = index++
            
            let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(space)
            
        }
        
        items.removeLast()
        toolBar.items = items
    }

}